package com.springdemo.shape;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * Using ApplicationContextAware Interface to get ApplicationContext Object,so that point can be initialized
 * Using BeanNameAware Interface to get the bean name for the current class
 * Using InitializingBean Interface to call init method before initializing bean
 * Using DisposableBean Interface to call destroy method before destroying bean
 * @author aditya_priyadarshi
 *
 */
public class Triangle implements Shape,ApplicationContextAware,BeanNameAware,InitializingBean,DisposableBean {

	private String type;
	private int height;
	private ApplicationContext context;
	private Point point;

	public Triangle() {
		
	}
	
	public Triangle(String type) {
		this.type = type;
	}

	public Triangle(int height) {
		this.height = height;
	}

	public Triangle(String type, int height) {
		this.type = type;
		this.height = height;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}
	
	public Point getPoint() {
		return point;
	}

	public void setPoint(){
		this.point = (Point) context.getBean("pointA");
	}
	
	public void setPoint(Point point) {
		this.point = point;
	}

	public void draw() {
		// Using ApplicationContextAware Interface to get ApplicationContext Object,so that point can be initialized
		setPoint();
		System.out.println("Drawing "+ getType()+" Triangle with height "+getHeight()+" at point "+point);
	}

	public void setApplicationContext(ApplicationContext context)
			throws BeansException {
		this.context = context;
		
	}
	
	// Used to get the bean name for the current class
	public void setBeanName(String beanName) {
		System.out.println("Bean Name:"+beanName);
		
	}

	public void afterPropertiesSet() throws Exception {
		System.out.println("IntializingBean init method called");
		
	}

	public void destroy() throws Exception {
		System.out.println("DisposableBean destroy method called");
		
	}
	
	public void myInit(){
		System.out.println("My init method");
	}
	
	public void myDestroy(){
		System.out.println("My Destroy method");
	}
}